package com.nf.health.app.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.R;
import com.nf.health.app.utils.DensityUtil;

public class ProgressBarView extends View {

	private float delay;//动态增量
	private float unitSize;//单位大小
	private float pbHeight;//进度条高度
//	private float top;
//	private float bottom;
	/**绘制矩形的长度*/
	private float totalLength;
	private int[] colors;
	private float secondDivider;
	/**
	 * 进度条均分的块数(手动修改)
	 */
	public static final int BLOCK = 6;
	private Paint textPaint;
	private Paint linePaint;
	private Paint paint;
	private Paint paintS;
	private int measureWidth;
	private int measureHeight;
	private float y;

	public ProgressBarView(Context context) {
		super(context);
		init(context);
	}
	public ProgressBarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public ProgressBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	private void init(Context context) {
		colors = new int[] { Color.parseColor("#854CB7"),
				Color.parseColor("#FFB125"), Color.parseColor("#85D3CC"),
				Color.parseColor("#FF506A"), };

		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Style.STROKE);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paintS = new Paint();
		paintS.setAntiAlias(true);
		paintS.setStyle(Style.STROKE);
		
		linePaint = new Paint();
		linePaint.setAntiAlias(true);
		linePaint.setStyle(Style.STROKE);
		DashPathEffect pathEffect = new DashPathEffect(new float[]{4,2}, 1f);
		linePaint.setPathEffect(pathEffect);
		linePaint.setColor(getResources().getColor(R.color.black8));
		//紫色#854CB7  黄色#FFB125  淡蓝色 #85D2CE 红色#FF506A
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize(DensityUtil.dip2px(context, 14));
		textPaint.setColor(Color.BLACK);
		
	}
	

	 private int measureWidth(int pWidthMeasureSpec) {  
	     int result = 0;  
	       
	     int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);  
	     int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);  
	       
	     switch (widthMode) {  
	     case MeasureSpec.AT_MOST:  
	     case MeasureSpec.EXACTLY:  
	         result = widthSize;  
	         break;  
     }  
	     return result;  
	 }  
	 private int measureHeight(int pHeightMeasureSpec) {  
		 int result = 0;  
		 
		 int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);  
		 int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);  
		 
		 switch (heightMode) {  
		 case MeasureSpec.AT_MOST:  
		 case MeasureSpec.EXACTLY:  
			 result = heightSize;  
			 break;  
		 }  
		 return result;  
	 }  

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		measureWidth = measureWidth(widthMeasureSpec);
		measureHeight = measureHeight(heightMeasureSpec);
		
		delay = ((float)measureWidth)/100;
		y = measureHeight/2;
		unitSize = ((float)measureWidth)/BLOCK;
		secondDivider = (BLOCK-2)*unitSize*26/100;//第二部分进度条的长度
		
		pbHeight = ((float)measureHeight)/5;//进度条的高度只需要三分之一就可以
//		top = 2*pbHeight;
//		bottom = pbHeight+top;
		
		paint.setStrokeWidth(pbHeight);
		paintS.setStrokeWidth(pbHeight);
		setMeasuredDimension(measureWidth, measureHeight);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setColor(colors[0]);
		canvas.drawLine(pbHeight, y, unitSize, y, paint);
		
		paint.setColor(colors[3]);
		canvas.drawLine((BLOCK-1)*unitSize, y, measureWidth-pbHeight, y, paint);
		
		paintS.setColor(colors[1]);
		canvas.drawLine(unitSize, y, unitSize +secondDivider, y, paintS);
		
		paintS.setColor(colors[2]);
		canvas.drawLine(unitSize +secondDivider, y, (BLOCK-1)*unitSize, y, paintS);
		
		drawFirstLine(canvas);
		drawFirstText(canvas);
		drawSecondLine(canvas);
		drawSecondText(canvas);
		drawThirdLine(canvas);
		drawThirdText(canvas);
	}
	
	private void drawFirstText(Canvas canvas) {
		//y表示文字写在的基准线位置。
		canvas.drawText("20.0", unitSize-25, pbHeight-5, textPaint);		
	}
	private void drawSecondText(Canvas canvas) {
		//y表示文字写在的基准线位置。
		canvas.drawText("22.6", unitSize +secondDivider-25, pbHeight-5, textPaint);
	}
	private void drawThirdText(Canvas canvas) {
		//y表示文字写在的基准线位置。
		canvas.drawText("30.0", (BLOCK-1)*unitSize-25, pbHeight-5, textPaint);		
	}
	private void drawThirdLine(Canvas canvas) {
		
		canvas.drawLine((BLOCK-1)*unitSize, pbHeight, (BLOCK-1)*unitSize, 2*pbHeight, linePaint);
		canvas.drawLine((BLOCK-1)*unitSize, 3*pbHeight, (BLOCK-1)*unitSize, 4*pbHeight, linePaint);
	}
	private void drawSecondLine(Canvas canvas) {
		canvas.drawLine(unitSize +secondDivider, pbHeight, unitSize +secondDivider, 2*pbHeight, linePaint);
		canvas.drawLine(unitSize +secondDivider, 3*pbHeight, unitSize +secondDivider, 4*pbHeight, linePaint);
	}
	/**
	 * 绘制进度条第一块和分割线
	 * @param canvas
	 */
	private void drawFirstLine(Canvas canvas) {
		
		canvas.drawLine(unitSize, pbHeight, unitSize, 2*pbHeight, linePaint);
		canvas.drawLine(unitSize, 3*pbHeight, unitSize, 4*pbHeight, linePaint);
	}
	public void refresh(){
		totalLength+=delay;
		postInvalidate();
	}

}
